﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sectors;
using Neat.Mathematics;
using SectorsDesigner.MapMaker;
namespace SectorsDesigner
{
    public partial class SectorPropertiesForm : Form
    {
        SectorInfo sector; SectorsMapMakerControl Designer;
        public SectorPropertiesForm(SectorInfo s, SectorsMapMakerControl m)
        {
            InitializeComponent();
            Designer = m;
            sector = s;
            Load(s);
        }

        public void Load(SectorInfo s)
        {
            FloorHeightBox.Value = (decimal)s.FloorHeight;
            CeilingHeightBox.Value = (decimal)s.CeilingHeight;
            FloorTextureBox.Text = s.FloorTexture;
            CeilingTextureBox.Text = s.CeilingTexture;
            FloorUVBox.Text = GeometryHelper.Vector2String(s.FloorUV);
            CeilingUVBox.Text = GeometryHelper.Vector2String(s.CeilingUV);
            UpperSectorBox.Text = s.UpperSector;
            LowerSectorBox.Text = s.LowerSector;
            DamageBox.Value = (decimal)s.DamagePoints;
            IdleTimeBox.Value = s.IdleTime;
            LightBox.Value = (decimal)s.LightIntensity;
            SectorTypeBox.Value = (int)s.SectorType;
            NameBox.Text = s.Name;
        }

        public void Save(SectorInfo s)
        {
            s.FloorHeight = (float)FloorHeightBox.Value;
            s.CeilingHeight = (float)CeilingHeightBox.Value;
            s.FloorTexture = FloorTextureBox.Text;
            s.CeilingTexture = CeilingTextureBox.Text;
            s.FloorUV = GeometryHelper.String2Vector(FloorUVBox.Text);
            s.CeilingUV = GeometryHelper.String2Vector(CeilingUVBox.Text);
            s.UpperSector = UpperSectorBox.Text;
            s.LowerSector = LowerSectorBox.Text;
            s.DamagePoints = (float)DamageBox.Value;
            s.IdleTime = (int)IdleTimeBox.Value;
            s.LightIntensity = (float)LightBox.Value;
            s.Name = NameBox.Text;
        }

        private void SaveButton_Click(object sender, EventArgs e)
        {
            Save(sector);
        }

        private void FloorUVBox_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                GeometryHelper.String2Vector(FloorUVBox.Text);
            }
            catch (Exception x)
            {
                e.Cancel = true;
                MessageBox.Show(x.Message, x.Source);
            }
        }

        private void CeilingUVBox_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                GeometryHelper.String2Vector(CeilingUVBox.Text);
            }
            catch (Exception x)
            {
                e.Cancel = true;
                MessageBox.Show(x.Message, x.Source);
            }
        }

        private void CancelButton_Click(object sender, EventArgs e)
        {

        }

        private void TextureFBtn_Click(object sender, EventArgs e)
        {
            UseTextureChooser(FloorTextureBox);
        }

        private void TextureCBtn_Click(object sender, EventArgs e)
        {
            UseTextureChooser(CeilingTextureBox);
        }

        void UseTextureChooser(TextBox t)
        {
            TextureChooserDialog d = new TextureChooserDialog(Designer);
            if (d.ShowDialog() == System.Windows.Forms.DialogResult.Cancel) return;
            t.Text = d.TextureNameBox.Text.Trim().ToLower();
        }
    }
}
